﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TuanDuiXiangMu.Areas.Back.Models;

namespace ShopBack.Web.Controllers
{
    public class TuPianShangChuanController : Controller
    {
        // GET: TuPianShangChuan
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult UploadPhoto()
        {
            try
            {
                HttpPostedFileBase postFile = Request.Files["file"];
                if (postFile != null)
                {
                    string fileExt = postFile.FileName.Substring(postFile.FileName.LastIndexOf(".") + 1); //文件扩展名，不含“.” 
                    string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "." + fileExt; //随机生成新的文件名 
                    string upLoadPath = "~/Content/img/"; //上传目录相对路径~表示网站根目录 
                    string fullUpLoadPath = Server.MapPath(upLoadPath); //上传目录的物理路径 
                    string newFilePath = upLoadPath + newFileName; //上传后的路径 
                    postFile.SaveAs(fullUpLoadPath + newFileName); //核心方法 
                    return Json(new UIResult(true, "", "img/" + newFileName));
                }
                else
                {
                    return Json(new UIResult(false, "未检测到文件数据"));
                }
            }
            catch (Exception ex)
            {

                return Json(new UIResult(false, "上传失败:" + ex.Message));
            }
        }
    }
}